package reader;

import common.DecF;
import common.PayException;
import common.Promotion;

// 会员读者类(继承自读者类并实现促销接口)
public class VIPReadernew extends Reader implements Promotion {
    private String readergrade;  // 读者身份级别
    private double percent;      // 会员折扣
    private int bonusPoints;     // 会员积分

    public VIPReadernew(String name, String grade) {
        super(name);  // 调用父类构造方法
        readergrade = grade;  // 设置读者身份
        bonusPoints = 0;
        setPercent();
    }

    public void setReadergrade(String grade) {
        readergrade = grade;
    }

    public String getReadergrade() {
        return readergrade;
    }

    public void promotion(String level) {  // 实现接口方法
        if (this.getBonusPoints() >= Promotion.promtpoints) {  // 积分达标
            switch (level) {
                case "VIP":
                    this.balance += 10;
                    break;  // VIP会员1000积分返利10元
                case "普通会员":
                    this.readergrade = "VIP";
                    break;  // 普通会员晋级为VIP
                case "非会员":
                    this.readergrade = "普通会员";
                    break;  // 非会员晋级为普通会员
            }
            bonusPoints -= Promotion.promtpoints;  // 扣除晋级所需积分
        } else {
            readergrade = level;  // 积分不达标，身份不变
        }
        setPercent();  // 重新设置折扣率
    }

    public void setPercent() {  // 设置折扣率
        if (readergrade.equals("VIP")) {
            percent = 0.8;  // VIP八折
        } else if (readergrade.equals("普通会员")) {
            percent = 0.9;  // 普通会员九折
        } else {
            percent = 1;    // 非会员无折扣
        }
    }

    public double getPercent() {
        return percent;
    }

    public void setBonusPoints(int point) {
        bonusPoints += point;
    }

    public int getBonusPoints() {
        return bonusPoints;
    }

    public void payRent(double rent) throws PayException {  // 支付租金
        if (balance < rent) {
            throw new PayException();  // 余额不足时抛出异常
        } else {
            balance = DecF.DecD(balance - rent);  // 保留2位小数
        }
    }

    public String toString() {
        return super.toString() + " 身份:" + readergrade + " 当前积分:" + bonusPoints +
                " 享有折扣率:" + percent;
    }
}
